package com.aefottt.struggle_star.adapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.aefottt.struggle_star.R;
import com.aefottt.struggle_star.activity.DataActivity;
import com.aefottt.struggle_star.bean.DataBean;
import com.aefottt.struggle_star.view.AchieveView;

import java.util.List;

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainViewHolder> {
    private final List<DataBean> dataBeans;
    private final Activity activity;

    public MainRecyclerAdapter(Activity activity, List<DataBean> dataBeans) {
        this.activity = activity;
        this.dataBeans = dataBeans;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_main_rv, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        DataBean dataBean = dataBeans.get(position);
        int isAchieved = dataBean.getIsAchieved();
        holder.achieveView.setAchieved(isAchieved);
        if (isAchieved == 0){
            holder.title.setText(dataBean.getDataTitle());
        }else if (isAchieved == 1){
            // 已完成，设置删除线及灰色字体
            SpannableStringBuilder spanTitle = new SpannableStringBuilder(dataBean.getDataTitle());
            spanTitle.setSpan(new StrikethroughSpan(), 0, dataBean.getDataTitle().length(),
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            spanTitle.setSpan(new ForegroundColorSpan(Color.GRAY), 0, dataBean.getDataTitle().length(),
                    Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            holder.title.setText(spanTitle);
        }
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(activity, DataActivity.class);
            intent.putExtra("DataBean", dataBean);
            activity.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity,
                    holder.title, "sharedTitle").toBundle());
        });
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    static class MainViewHolder extends RecyclerView.ViewHolder{
        AchieveView achieveView;
        TextView title;
        public MainViewHolder(@NonNull View itemView) {
            super(itemView);
            achieveView = itemView.findViewById(R.id.achieved_view_main_rv);
            title = itemView.findViewById(R.id.tv_main_rv_title);
        }
    }
}
